<?php

function load_properties($propertiesFile) {
	$data = file_get_contents ($propertiesFile);
	$result = array();
	$lines = split("\n", $data);
	$key = "";
	$isWaitingOtherLine = false;
	foreach ($lines as $i => $line) {
		if (empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0))
			continue;

		if (!$isWaitingOtherLine) {
			$key = substr($line, 0, strpos($line, '='));
			$value = substr($line, strpos($line, '=')+1, strlen($line));
		}
		else {
			$value .= $line;
		}	

		/* Check if ends with single '\' */
		if (strrpos($value, "\\") === strlen($value)-strlen("\\")) {
			$value = substr($value,0,strlen($value)-1)."\n";
			$isWaitingOtherLine = true;
		}
		else {
			$isWaitingOtherLine = false;
		}

		$result[$key] = trim($value);
		unset($lines[$i]);
	}

	return $result;
}

function do_post_request($url, $data, $optional_headers = null)
  {
     $params = array('http' => array(
                  'method' => 'POST',
                  'content' => $data
               ));
     if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
     }
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
     }
     $response = @stream_get_contents($fp);
     if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
     }
     return $response;
  }


function getConstantsAsArray($name_pattern) {
	$array = get_defined_constants(true);
	$results = array();
	foreach ($array[user] as $i => $value) {
		if (preg_match("/".$name_pattern."/", $i)) {
			$results[$i] = $value;
		}
	}
	return $results;
}

function getSortIcon($is_sort, $asc) {
	if(!$is_sort) return "<img src='./img/icons/updown.png'>";
	else if($asc) return "<img src='./img/icons/asc.png'>";
	else return "<img src='./img/icons/desc.png'>";
}

function get_page_menu($page_id) {
  global $group_menu;
  for($n=0; $n < count($group_menu); $n++) {
    if(in_array($page_id, $group_menu[$n]["pages"])) {
      return $n;
    }
  }
  return -1;
}

function get_url_of_page($page_id) {
  global $pages;
  $params = "";
  if(isset($pages[$page_id]["params"])){
    for($i = 0; $i < count($pages[$page_id]["params"]); $i++){
      $params.=add_par($pages[$page_id]["params"][$i]);
    }
  }
  return urlencode(DOMAIN."/".REDIRECT.$page_id.ADD_PAR."menu=".get_page_menu($page_id).$params);
}

function redirect_str($page_id) {
  global $pages;
  $params = "";
  if(isset($pages[$page_id]["params"])){
    for($i = 0; $i < count($pages[$page_id]["params"]); $i++){
      $params.=add_par($pages[$page_id]["params"][$i]);
    }
  }
  return REDIRECT.$page_id.ADD_PAR."menu=".get_page_menu($page_id).$params;
}

function redirect_str_header($page_id) {
  global $pages;
  $params = "";
  if(isset($pages[$page_id]["params"])){
    for($i = 0; $i < count($pages[$page_id]["params"]); $i++){
      $params.="&".$pages[$page_id]["params"][$i];
    }
  }
  return REDIRECT.$page_id."&menu=".get_page_menu($page_id).$params;
}

function add_par($param) {
  return ADD_PAR.$param;
}

function action($a) {
  return add_par("action=".$a);
}

function redirect($page_id, $args) {
  Header("Location: ".redirect_str_header($page_id).$args);
}

function isdir($dir){
  $pole = explode(".", $dir);
  if($pole[1] == "") return true;
  return false;
}

function isfile($file){
  $pole = explode(".", $file);
  if($pole[1] != "" && $pole[1] != ".") return true;
  return false;
}

function removeDiacritics($text){
	$tbl = array("\xc3\xa1"=>"a","\xc3\xa4"=>"a","\xc4\x8d"=>"c","\xc4\x8f"=>"d","\xc3\xa9"=>"e","\xc4\x9b"=>"e","\xc3\xad"=>"i","\xc4\xbe"=>"l","\xc4\xba"=>"l","\xc5\x88"=>"n","\xc3\xb3"=>"o","\xc3\xb6"=>"o","\xc5\x91"=>"o","\xc3\xb4"=>"o","\xc5\x99"=>"r","\xc5\x95"=>"r","\xc5\xa1"=>"s","\xc5\xa5"=>"t","\xc3\xba"=>"u","\xc5\xaf"=>"u","\xc3\xbc"=>"u","\xc5\xb1"=>"u","\xc3\xbd"=>"y","\xc5\xbe"=>"z","\xc3\x81"=>"A","\xc3\x84"=>"A","\xc4\x8c"=>"C","\xc4\x8e"=>"D","\xc3\x89"=>"E","\xc4\x9a"=>"E","\xc3\x8d"=>"I","\xc4\xbd"=>"L","\xc4\xb9"=>"L","\xc5\x87"=>"N","\xc3\x93"=>"O","\xc3\x96"=>"O","\xc5\x90"=>"O","\xc3\x94"=>"O","\xc5\x98"=>"R","\xc5\x94"=>"R","\xc5\xa0"=>"S","\xc5\xa4"=>"T","\xc3\x9a"=>"U","\xc5\xae"=>"U","\xc3\x9c"=>"U","\xc5\xb0"=>"U","\xc3\x9d"=>"Y","\xc5\xbd"=>"Z");
    return strtr($text, $tbl);
}

function is_mail ($mail){
//tohle kontroluje jestli je zadana posloupnost nejakych inteligentnich znaku, pak zavinac, pak zase znaky a nakonec nejaka domena
  if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $mail)){
    return true;
  }
  return false;
}

function convert($str){
  return htmlspecialchars(trim($str));
}

function setFocus($id, $page){
  if($id == $page){
    echo " class='focused' ";
  }
}

function random_code($length) {
  $code = "";
  for($i=0; $i<$length; $i++) $code = $code.rand(0,9);
  return $code;
}

function codegen(){
  return random_code(5);
}

function br2nl($string){
  $return=eregi_replace('<br[[:space:]]*/?'.
    '[[:space:]]*>',chr(13).chr(10),$string);
  return $return;
}

function draw_roster_picture($img_path) {
	$width = $_POST['width'];
	$height = $_POST['height'];
	$image = imagecreatetruecolor( $width ,$height );
	$background = imagecolorallocate( $image ,0 , 0 , 0 );
	//Copy pixels
	$i = 0;
	for($x = 0; $x <= $width; $x++){
		$data = explode(",", $_POST["col$x"]);
		for($y = 0; $y <= $height; $y++){
			$int = hexdec($data[$y]);
			$color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
			imagesetpixel ( $image , $x , $y , $color );
		}
	}
	
	if (file_exists($img_path)) {
		unlink($img_path);
	}
	imagepng($image, $img_path);
	imagedestroy($image);
}

?>